﻿using System.Collections.Generic;
using UnityEngine;

namespace EchoFramework
{
    /// <summary>
    /// 有限状态机状态 抽象类
    /// </summary>
    public abstract class State
    {
        /// <summary>
        /// 有限状态机管理器
        /// </summary>
        protected StateMachine stateMachine;
        /// <summary>
        /// 属于的对象
        /// </summary>
        protected GameObject owner;
        /// <summary>
        /// 所属对象的Transform
        /// </summary>
        protected Transform ownerTrans => owner?.transform;
        /// <summary>
        /// 状态转换字典
        /// key:过渡条件
        /// value:状态枚举
        /// </summary>
        protected Dictionary<FSMTransition, FSMState> trans2stateEnumDict;
        /// <summary>
        /// 自身状态枚举
        /// </summary>
        public FSMState FsmState;
        /// <summary>
        /// 进度状态时调用
        /// </summary>
        public abstract void OnEnter();
        /// <summary>
        /// 状态持续时调用
        /// </summary>
        public abstract void OnUpdate();
        /// <summary>
        /// 状态持续时调用(物理)
        /// </summary>
        public abstract void OnFixedUpdate();
        /// <summary>
        /// 退出状态时调用
        /// </summary>
        public abstract void OnExit();

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="stateMachine">有限状态机管理器</param>
        /// <param name="fsmState">自身状态</param>
        /// <param name="owner">当前状态属于的对象</param>
        public State(StateMachine stateMachine, FSMState fsmState, GameObject owner)
        {
            this.stateMachine = stateMachine;
            this.FsmState = fsmState;
            this.owner = owner;
            trans2stateEnumDict = new Dictionary<FSMTransition, FSMState>();
        }

        /// <summary>
        /// 添加过渡条件
        /// </summary>
        /// <param name="fsmTransition">过渡条件</param>
        /// <param name="fsmNextFsmState">下一个状态</param>
        public void AddTransition(FSMTransition fsmTransition, FSMState fsmNextFsmState)
        {
            if (fsmTransition == FSMTransition.Null || trans2stateEnumDict.ContainsKey(fsmTransition))
            {
                GameLogger.LogWarning(string.Format("{0} 添加的过渡条件为Null或已存在该过渡条件", owner.name), owner);
                return;
            }
            trans2stateEnumDict.Add(fsmTransition, fsmNextFsmState);
        }

        /// <summary>
        /// 删除过渡条件
        /// </summary>
        /// <param name="fsmTransition">过渡条件</param>
        public void DeleteTransition(FSMTransition fsmTransition)
        {
            if (fsmTransition == FSMTransition.Null || !trans2stateEnumDict.ContainsKey(fsmTransition))
            {
                GameLogger.LogWarning(string.Format("{0} 删除的过渡条件为Null或该过渡条件不存在", owner.name), owner);
            }
        }

        /// <summary>
        /// 获取过渡条件指向的下一个状态
        /// </summary>
        /// <param name="fsmTransition">过渡条件</param>
        /// <returns>下一个状态</returns>
        public FSMState GetNextState(FSMTransition fsmTransition)
        {
            if (fsmTransition == FSMTransition.Null)
            {
                GameLogger.LogError(string.Format("{0} 传入的过渡条件为Null！", owner.name), owner);
                return FSMState.Null;
            }
            if (!trans2stateEnumDict.ContainsKey(fsmTransition))
            {
                GameLogger.LogError(string.Format("{0} 该状态不存在过渡条件 {1}", owner.name, fsmTransition), owner);
                return FSMState.Null;
            }
            return trans2stateEnumDict[fsmTransition];
        }
    }
}
